package com.eastcom.bm.dataprocess.noticeserver;

import java.util.Hashtable;
import java.util.Map;

public class JMSMessage extends AlarmMessage{
	
	private NoticeMessage noticeMessage;
	
	private String initial_context_factory;
	
	private String provider_url;
	
	private String security_authentication;
	
	private String security_principal;
	
	private String security_credentials;
	
	private String destination_name;
	
	private String destination_factory;
	
	private String subscribe_durable;
	
	private String destination_type;
	
	private String client_id;

	private Hashtable ctxInfoMap;
	
	private Map parammap;
	
	public JMSMessage(){
		
	}
	
	public JMSMessage(Map jmsinfomap){
		this.parammap=jmsinfomap;
		this.parseJMSInfoMap(jmsinfomap);
	}
	
	private void parseJMSInfoMap(Map jmsinfomap){
		ctxInfoMap=new Hashtable();
		
		this.uuid=(String) jmsinfomap.get("uuid");
		this.initial_context_factory=(String)jmsinfomap.get("initial_context_factory");
		this.provider_url=(String)jmsinfomap.get("provider_url");
		this.security_authentication=(String)jmsinfomap.get("security_authentication");
		this.security_principal=(String)jmsinfomap.get("security_principal");
		this.security_credentials=(String)jmsinfomap.get("security_credentials");
		this.destination_name=(String)jmsinfomap.get("destination_name");
		this.destination_factory=(String)jmsinfomap.get("destination_factory");
		this.subscribe_durable=(String)jmsinfomap.get("subscribe_durable");
		this.client_id=(String)jmsinfomap.get("client_id");
		
		initctxInfoMap();
	}
	
	public void initctxInfoMap(){
		ctxInfoMap.put("java.naming.factory.initial", initial_context_factory);
		ctxInfoMap.put("java.naming.provider.url", provider_url);
		ctxInfoMap.put("java.naming.security.authentication", security_authentication);
		ctxInfoMap.put("java.naming.security.principal", security_principal);
		ctxInfoMap.put("java.naming.security.credentials", security_credentials);
		ctxInfoMap.put("java.naming.referral", "throw");
	}
	
	public String getInfo(){
	      String result = null;
	      String message =this.getSendMessage();
	      
	      if(false){
	          //MyLogger.logger.warn("MESSAGE FOR UUID "+oid+ " is null, and JMS SENDER won't send trap for message @"+this);
	          //return null;
	      }
	      else{
	            String uuid = this.getUuid();
	            if(uuid==null){
	                //MyLogger.logger.warn("UUID of Sender is null, and JMS SENDER won't send trap for message @"+this);
	                //return null;
	            }
	            
	            //Map var = this.getParammap();
	            /*
	            System.out.println("in JMS SENDER, varible is "+ var);
	            if(var!=null){
	                for(Iterator it = var.keySet().iterator(); it.hasNext();)
	                {
	                    String name = (String)it.next();
	                    String value = (String)var.get(name);
	                    
	                    message = message.replaceAll("(\\$"+name+")",value);
	                }
	            }
	            */
	            
	            StringBuffer sb = new StringBuffer();
	            sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	            sb.append("<pdu>");
	            sb.append("<operationType>");
	            //sb.append("INSERT");
	            sb.append(this.noticeMessage.getOperationType());
	            sb.append("</operationType>");
	            
	            int severity=6-this.noticeMessage.getAlarmLevel();
	            sb.append("<sourceId>");
	            //sb.append(noticeMessage.getIndicatoruuid()+"_"+noticeMessage.getDimension()+"_"+severity);
	            sb.append(noticeMessage.getIndicatoruuid()+"_"+noticeMessage.getDimension());
	            sb.append("</sourceId>");
	            
	            sb.append("<eventId>");
	            if("INSERT".equals(noticeMessage.getOperationType()))
	            	sb.append(severity==2?"IPNET-SMS-CRITICAL-ALARM":"IPNET-SMS-ALARM");
	            else
	                sb.append("IPNET-SMS-OK-ALARM");
	            
	            sb.append("</eventId>");
	            
	            sb.append("<title>");
	            sb.append(noticeMessage.getTitle());
	            sb.append("</title>");
	            
	            sb.append("<severity>");
	            sb.append(severity);
	            sb.append("</severity>");
	            
	            sb.append("<facility>");
	            sb.append("6");
	            sb.append("</facility>");

	            sb.append("<agent>");
	            sb.append("10.201.36.36");
	            sb.append("</agent>");
	            
	            sb.append("<agentType>");
	            sb.append("2");
	            sb.append("</agentType>");
	            
	            sb.append("<OBJNAME>");
	            sb.append("106582660[ʡIOD]");
	            sb.append("</OBJNAME>");
	            
	            sb.append("<message>");
	            sb.append(message);
	            sb.append("</message>");
	            
	            sb.append("<keyValue>");
	            sb.append(noticeMessage.getIndicatoruuid()+"_"+noticeMessage.getDimension());
	            sb.append("</keyValue>");
	            
	            sb.append("<counts>");
	            sb.append("1");
	            sb.append("</counts>");
	            
	            sb.append("<collectionType>");
	            sb.append("3");
	            sb.append("</collectionType>");
	            
	            sb.append("<probeName>");
	            sb.append("test");
	            sb.append("</probeName>");
	            
	            //sb.append("<message>");
	            //sb.append(sendMessage);
	            //sb.append("</message>");
	            
	            sb.append("</pdu>");
	            
	            result = sb.toString();
	        }
	        
	        //System.out.println("in JMS SENDER, message final is "+ result);
	        this.sendMessage=result;
	        return result;
	        
	}
	
	public String getClient_id() {
		return client_id;
	}

	public void setClient_id(String client_id) {
		this.client_id = client_id;
	}

	public String getDestination_factory() {
		return destination_factory;
	}

	public void setDestination_factory(String destination_factory) {
		this.destination_factory = destination_factory;
	}

	public String getDestination_name() {
		return destination_name;
	}

	public void setDestination_name(String destination_name) {
		this.destination_name = destination_name;
	}

	public String getInitial_context_factory() {
		return initial_context_factory;
	}

	public void setInitial_context_factory(String initial_context_factory) {
		this.initial_context_factory = initial_context_factory;
	}

	public String getProvider_url() {
		return provider_url;
	}

	public void setProvider_url(String provider_url) {
		this.provider_url = provider_url;
	}

	public String getSecurity_authentication() {
		return security_authentication;
	}

	public void setSecurity_authentication(String security_authentication) {
		this.security_authentication = security_authentication;
	}

	public String getSecurity_credentials() {
		return security_credentials;
	}

	public void setSecurity_credentials(String security_credentials) {
		this.security_credentials = security_credentials;
	}

	public String getSecurity_principal() {
		return security_principal;
	}

	public void setSecurity_principal(String security_principal) {
		this.security_principal = security_principal;
	}

	public String getSubscribe_durable() {
		return subscribe_durable;
	}

	public void setSubscribe_durable(String subscribe_durable) {
		this.subscribe_durable = subscribe_durable;
	}

	public Hashtable getCtxInfoMap() {
		return ctxInfoMap;
	}

	public void setCtxInfoMap(Hashtable ctxInfoMap) {
		this.ctxInfoMap = ctxInfoMap;
	}

	public String getDestination_type() {
		return destination_type;
	}

	public void setDestination_type(String destination_type) {
		this.destination_type = destination_type;
	}

	public Map getParammap() {
		return parammap;
	}

	public void setParammap(Map parammap) {
		this.parammap = parammap;
	}

	public NoticeMessage getNoticeMessage() {
		return noticeMessage;
	}

	public void setNoticeMessage(NoticeMessage noticeMessage) {
		this.noticeMessage = noticeMessage;
	}
	
}
